Code
import os
'pip install numpy')
os.system('pip install matplotlib')
os.system('pip install pandas')
os.system('pip install seaborn')
os.system('pip install statsmodels') os.system(
Tony Duan
December 29, 2022
https://wesmckinney.com/book/python-basics.html
Python for Data Analysis by Wes Mckinney
{exec,command='bash'} echo 'hello'
Essential Python Libraries: NumPy/pandas/matplotlib/SciPy/scikit-learn/statsmodels
install package
Import Conventions
[0.7810594371163568,
-0.7334955362847929,
0.5167768619691568,
0.9043756983157261,
-1.243885234931727,
0.16636014847333624,
1.1251843432345972]
An important characteristic of the Python language is the consistency of its object model. Every number, string, data structure, function, class, module, and so on exists in the Python interpreter in its own “box,” which is referred to as a Python object. Each object has an associated type (e.g., integer, string, or function) and internal data.
number and string
list:
Control Flow:f, elif, and else
for loops
while loops
pass
positive!
range
it start with 0 position
a tuple of tuples:
!!!once the tuple is created it’s not possible to modify!!!
append element at the end
insert element by position
if error then return except
array([[ 1.5, -0.1, 3. ],
[ 0. , -3. , 6.5]])
state | year | pop | |
---|---|---|---|
0 | Ohio | 2000 | 1.5 |
1 | Ohio | 2001 | 1.7 |
2 | Ohio | 2002 | 3.6 |
3 | Nevada | 2001 | 2.4 |
4 | Nevada | 2002 | 2.9 |
5 | Nevada | 2003 | 3.2 |
state | year | pop | |
---|---|---|---|
0 | Ohio | 2000 | 1.5 |
1 | Ohio | 2001 | 1.7 |
2 | Ohio | 2002 | 3.6 |
3 | Nevada | 2001 | 2.4 |
4 | Nevada | 2002 | 2.9 |
state | year | pop | |
---|---|---|---|
1 | Ohio | 2001 | 1.7 |
2 | Ohio | 2002 | 3.6 |
3 | Nevada | 2001 | 2.4 |
4 | Nevada | 2002 | 2.9 |
5 | Nevada | 2003 | 3.2 |
https://wesmckinney.com/book/
---
title: "【Reading】Python for Data Analysis Part 1 Chapter 1-8"
author: "Tony Duan"
date: "2022-12-29"
categories: [Book]
execute:
warning: false
error: false
format:
html:
toc: true
code-fold: show
code-tools: true
---
https://wesmckinney.com/book/python-basics.html
Python for Data Analysis by Wes Mckinney

```{exec,command='bash'}
echo 'hello'
```
## 1 Preliminaries
Essential Python Libraries: NumPy/pandas/matplotlib/SciPy/scikit-learn/statsmodels
**install package**
```{python}
#| eval: false
import os
os.system('pip install numpy')
os.system('pip install matplotlib')
os.system('pip install pandas')
os.system('pip install seaborn')
os.system('pip install statsmodels')
```
**Import Conventions**
```{python}
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import statsmodels as sm
```
## 2 Python Language Basics, IPython, and Jupyter Notebooks
```{python}
print("Hello world")
```
```{python}
import numpy as np
data = [np.random.standard_normal() for i in range(7)]
data
```
An important characteristic of the Python language is the consistency of its object model. Every number, string, data structure, function, class, module, and so on exists in the Python interpreter in its own "box," which is referred to as a Python object. Each object has an associated type (e.g., integer, string, or function) and internal data.

number and string
```{python}
v1=123
v2='abc'
type(v1)
type(v2)
```
list:
```{python}
a = [1, 2, 3]
a
a.append(4)
a
type(a)
```
**Control Flow:f, elif, and else**
```{python}
x = -5
if x < 0:
print("It's negative")
```
**for loops**
```{python}
sequence = [1, 2, None, 4, None, 5]
total = 0
for value in sequence:
if value is None:
continue
total += value
```
**while loops**
```{python}
x = 256
total = 0
while x > 0:
if total > 500:
break
total += x
x = x // 2
```
**pass**
```{python}
if x < 0:
print("negative!")
elif x == 0:
# TODO: put something smart here
pass
else:
print("positive!")
```
**range**
```{python}
list(range(10))
```
```{python}
list(range(0, 20, 2))
```
## 3 Built-In Data Structures, Functions, and Files
### Tuple
```{python}
tup = (4, 5, 6)
tup
```
it start with 0 position
```{python}
tup[0]
```
a tuple of tuples:
```{python}
nested_tup = ((4, 5, 6), (7, 8))
nested_tup
```
!!!once the tuple is created it’s not possible to modify!!!
### list
```{python}
a_list = [2, 3, 7, None]
a_list
```
```{python}
gen = range(10)
list(gen)
```
append element at the end
```{python}
a_list.append("dwarf")
a_list
```
insert element by position
```{python}
a_list.insert (1,"new")
a_list
```
### Dictionary
### Set
### Functions
```{python}
def function_without_return(x):
print(x)
function_without_return('hello')
```
### Errors and Exception Handling
if error then return except
```{python}
def attempt_float(x):
try:
return float(x)
except:
return x
```
```{python}
attempt_float("1.2345")
```
```{python}
attempt_float("something")
```
### Files and the Operating System
## 4 NumPy Basics: Arrays and Vectorized Computation
```{python}
import numpy as np
data = np.array([[1.5, -0.1, 3], [0, -3, 6.5]])
data
```
```{python}
data * 10
```
```{python}
data.shape
data.dtype
```
## 5 Getting Started with pandas
```{python}
import numpy as np
import pandas as pd
```
```{python}
data = {"state": ["Ohio", "Ohio", "Ohio", "Nevada", "Nevada", "Nevada"],
"year": [2000, 2001, 2002, 2001, 2002, 2003],
"pop": [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
frame
```
```{python}
frame.head()
```
```{python}
frame.tail()
```
```{python}
frame["state"]
```
```{python}
frame.state
```
## 6 Data Loading, Storage, and File Formats
## 7 Data Cleaning and Preparation
## 8 Data Wrangling: Join, Combine, and Reshape
## Reference
https://wesmckinney.com/book/